package com.example.Cairodio;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

import com.google.android.maps.OverlayItem;

@SuppressWarnings("rawtypes")
public class ItemizedOverlay extends com.google.android.maps.ItemizedOverlay {

	private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
	Activity mapActivity;
	static final int _AudioClipsActivityIntent = 1;

	/**
	 * This the overlay constructor
	 * 
	 * @param defaultMarker: Drawable
	 * 
	 * @author Mariam
	 */
	public ItemizedOverlay(Drawable defaultMarker, Activity mainActivity) {
		super(boundCenterBottom(defaultMarker));
		this.mapActivity=mainActivity;
	}

	@Override
	protected OverlayItem createItem(int i) {
		return mOverlays.get(i);
	}

	/**
	 * This method returns the size of the overlay
	 * 
	 * @author Mariam
	 */
	@Override
	public int size() {
		return mOverlays.size();
	}
	
	/**
	 * This method adds an overlay
	 * @param overlay
	 * 
	 * @author Mariam
	 */
	public void addOverlay(OverlayItem overlay) {
		mOverlays.add(overlay);
		populate();
	}

	
	 /**
	  * @author Mariam
	  * 
	  * Removes overlay item i
	  * @param i
	  */
    public void removeItem(int i){
        mOverlays.remove(i);
        populate();
    }
    
	/**
	 * This method directs to the touristic place's audio clip
	 * when the user taps on it.
	 * 
	 * @author Mariam
	 */
	@Override
	protected boolean onTap(int i) {

		if(mOverlays.size()>1){
		String placeName = mOverlays.get(i).getTitle();
		int placeId = Integer.parseInt(mOverlays.get(i).getSnippet());

		Bundle b = new Bundle();
		b.putInt("placeId", placeId);
		b.putString("placeName", placeName);
		 Intent intentAudioClipsActivity = new Intent(CairodioMapActivity.context,AudioClipsActivity.class);
		 intentAudioClipsActivity.putExtras(b);
         mapActivity.startActivityForResult(intentAudioClipsActivity,  _AudioClipsActivityIntent);
         
		/*Intent intent = new Intent(CairodioMapActivity.context,
				AudioClipsActivity.class);
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
		intent.putExtras(b);
		CairodioMapActivity.context.startActivity(intent);*/
		}

		return (true);
	}

}
